﻿#include "tservice.h"

#include "../../include/itframework.h"
#include "../../include/itlogger.h"
#include "../../include/itpublicmgr.h"
#include "../../include/ituimgr.h"
#include "../../include/itmodelbase.h"

TmUiService::TmUiService()
{
    m_pFramework = NULL;
    m_pLogger    = NULL;
    m_pPublicMgr = NULL;
    m_pUiMgr     = NULL;
    m_mutex     = NULL;
}

TmUiService::~TmUiService()
{

}

TRESULT TmUiService::Init()
{
    connect(this, &TmUiService::sigPostTrdMsg, this, &TmUiService::slotGetTrdMsg);
    this->moveToThread(&m_thread);
    m_thread.start();
    return T_OK;
}

TRESULT TmUiService::UnInit(int nTime)
{
    m_thread.quit();
    if(m_thread.wait(nTime))
    {
        m_thread.terminate();
        m_thread.wait(nTime);
    }
    SAFE_DELETE(m_mutex);
    return T_OK;
}

TRESULT TmUiService::CreateTask(TArgs **ppTask, long lInArgsSize, long lOutArgsSize)
{
    *ppTask = new TArgs;
    if(NULL == *ppTask)
        return T_FALSE;
    memset(*ppTask,0,sizeof(TArgs));
    if(0 < lInArgsSize)
    {
        (*ppTask)->CommandArgs = (TObject*)malloc(lInArgsSize);
        if((*ppTask)->CommandArgs)
            memset((char*)(*ppTask)->CommandArgs,0,lInArgsSize);
    }

    if(0 < lOutArgsSize)
    {
        (*ppTask)->CommandRst = (TObject*)malloc(lOutArgsSize);
        if((*ppTask)->CommandRst)
            memset((char*)(*ppTask)->CommandRst,0,lOutArgsSize);
    }

    return T_OK;
}

TRESULT TmUiService::AddTask(TArgs *pTask, int nTask)
{
    if(!pTask)
        return T_FALSE;

    TRESULT hr = PostThreadMsg(nTask,NULL,(void*)pTask);
    return hr;
}

TRESULT TmUiService::PostThreadMsg(unsigned long ulMsg, void *wParam, void *lParam)
{
    qDebug() << "TmUiService PostThradMsg threadId=" << QThread::currentThreadId();
    emit sigPostTrdMsg(ulMsg, wParam, lParam);
    return T_OK;
}

TRESULT TmUiService::SetFramework(ITFramework *pFrm)
{
    m_pFramework = pFrm;
    m_pFramework->GetLogger(&m_pLogger);
    return T_OK;
}

void TmUiService::slotGetTrdMsg(unsigned long ulMsg, void *wParam, void *lParam)
{
    qDebug() << "TmUiService::slotGetTrdMsg threadId=" << QThread::currentThreadId();
    wParam = NULL;
    if(NULL == m_mutex)
        m_mutex = new QMutex;
    m_mutex->lock();
    TArgs* pArgs = (TArgs*)lParam;
    switch(ulMsg)
    {
    case WM_EXE_INVOKE:
    {
        m_pFramework->GetPublicMgr(&m_pPublicMgr);
        m_pFramework->GetUiMgr(&m_pUiMgr);
        if(m_pPublicMgr && m_pUiMgr)
        {
            ITModelBase *pModel = NULL;
            qDebug() << "TmUiService:PublicId=" << pArgs->PublicID << "UiId=" << pArgs->UiID << "commandId=" << pArgs->CommandID;
            if(SUCCEEDT(m_pPublicMgr->GetPublicFromId(pArgs->PublicID,&pModel))
                    || SUCCEEDT(m_pUiMgr->GetUiFromId(pArgs->UiID,&pModel)))
            {
                if(NULL != pModel)
                {
                    TArgs inArgs;
                    inArgs.CommandID = pArgs->CommandID;
                    inArgs.CommandArgs = pArgs->CommandArgs;
                    inArgs.RstModelID = pArgs->RstModelID;
                    inArgs.RstCommandID = pArgs->RstCommandID;
                    inArgs.CommandRst = pArgs->CommandRst;
                    pModel->Invoke(&inArgs);
                    if(inArgs.CommandRst)
                    {
                        ITModelBase *pRstModel = NULL;
                        if(SUCCEEDT(m_pPublicMgr->GetPublicFromId(pArgs->RstModelID,&pRstModel))
                                || SUCCEEDT(m_pUiMgr->GetUiFromId(pArgs->RstModelID,&pRstModel)))
                        {
                            if(NULL != pRstModel)
                            {
                                TArgs inRstArgs;
                                inRstArgs.CommandID = inArgs.RstCommandID;
                                inRstArgs.CommandArgs = inArgs.CommandRst;
                                pRstModel->Invoke(&inRstArgs);
                            }
                        }
                    }
                }
                else
                {
                    qDebug() << "TmUiService::slotGetTrdMsg not find pModel";
                }
            }
        }
        SAFE_FREE(pArgs->CommandArgs);
        SAFE_FREE(pArgs->CommandRst);
        SAFE_DELETE(lParam);
    }
        break;
    default:
        break;
    }
    m_mutex->unlock();
}
